package com.onlineMIS.action.headQ.inventoryFlow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.headQ.inventoryFlow.HeadQInventoryStoreDAOImpl;
import com.onlineMIS.ORM.DAO.headQ.inventoryFlow.HeadqInventoryFlowService;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ComboboxVO;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTag;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTagGroup;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTagVO;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadQInventoryStore;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.loggerLocal;
import com.onlineMIS.converter.JSONSQLDateConverter;
import com.onlineMIS.converter.JSONUtilDateConverter;
import com.opensymphony.xwork2.ActionContext;

/**
 * action to 
 * @author fredo
 *
 */
@SuppressWarnings("serial")
public class HeadqInventoryFlowJSONAction extends HeadqInventoryFlowAction{

	@Autowired
	private HeadqInventoryFlowService flowService;
	private JSONObject jsonObject;
	private JSONArray jsonArray;
	private Map<String,Object> jsonMap = new HashMap<String, Object>();

	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	
	
	public JSONArray getJsonArray() {
		return jsonArray;
	}
	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}

	
	/**
	 * 根据库房和货品归属查询库存信息
	 * @return
	 */
	public String getInventoryFlowEles() {
		loggerLocal.info("HeadqInventoryFlowJSONAction - getInventoryFlowEles");
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		Response response = new Response();

		try {
			response = headqInventoryService.getHeadqInventory(formBean.getParentId(), formBean.getStoreId(),
					formBean.getYearId(), formBean.getQuarterId(), formBean.getBrandId(), formBean.getCategoryLevelId(),
					formBean.getCategoryId(), formBean.getRptTypeId(), formBean.getBelongTo(),loginUserInfor);
		} catch (Exception e) {
			e.printStackTrace();
			response.setFail(e.getMessage());
		}

		try {
			jsonArray = JSONArray.fromObject(response.getReturnValue());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "jsonArray";
	}
	
	
	/**
	 * 根据库房和货品归属查询进销存变动统计
	 * @return
	 */
	public String getInventoryChangeDetails() {
		loggerLocal.info("HeadqInventoryFlowJSONAction - getInventoryChangeDetails");
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		Response response = new Response();
		
		try {
			response = headqInventoryService.getHeadqInventoryChangeDetails(formBean.getParentId(),formBean.getStartDate(),formBean.getEndDate(), formBean.getStoreIds(),
					formBean.getYearId(), formBean.getQuarterId(), formBean.getBrandId(), formBean.getCategoryLevelId(),
					formBean.getCategoryId(), formBean.getRptTypeId(), formBean.getBelongTo(),loginUserInfor);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			jsonArray = JSONArray.fromObject(response.getReturnValue());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "jsonArray";
	}
	

	
	/**
	 * 查询客户标签分组json对象集合
	 * @return
	 */
	public String selectInventoryStores() {
		loggerLocal.info("HeadQCustMgmtJSONAction.listCusTags");

		Response response = new Response();
		try {
			List<ComboboxVO> vos = headqInventoryService.selectInventoryStores();

			JsonConfig jsonConfig = new JsonConfig();
    		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JSONUtilDateConverter());  
    		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JSONSQLDateConverter());  
			jsonArray = JSONArray.fromObject(vos,jsonConfig);
					
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}

		return "jsonArray";
	}
	
	/**
	 * 在inventory flow order上 scan barcode
	 * @return
	 */
	public String scanByBarcodeInvenFlowOrder(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : HeadqInventoryFlowJSONAction.scanByBarcodeInvenFlowOrder");
		
		Response response = new Response();
		try {
			
			response = headqInventoryService.scanByBarcodeInvenOrder(formBean.getPb().getBarcode(),  formBean.getIndexPage(), formBean.getFromSrc(), formBean.getStoreId());

		}  catch(Exception e){
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		} finally {
			jsonObject = JSONObject.fromObject(response);
		}
		
		return SUCCESS;
	}
	
	
	/**
	 * 查询总部库存单据
	 * @return
	 */
	public String searchInvenOrder(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("HeadqInventoryFlowJSONAction - searchInvenOrder");
		Response response = new Response();
   			
		response = headqInventoryService.searchInvenFlowOrders(formBean,this.page,this.rows, this.sort, this.order,loginUserInfor );
		
		if (response.isSuccess()){
			jsonMap = (Map<String, Object>)response.getReturnValue();
		}
		
		//to excludes the set and list inforamtion
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes( new String[]{"productSet","productList","myChainStore","employeeUnder_Set","chainUserFunctions","toChainStore","userFunction_Set"} );
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JSONUtilDateConverter());  
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JSONSQLDateConverter());  
		try{
			   jsonObject = JSONObject.fromObject(jsonMap,jsonConfig);
  
			} catch (Exception e){
				loggerLocal.error(e);
			}
			
		return SUCCESS;		
	}
		
	
	/**
	 * 库存扎帐 
	 * @return
	 */
	public String getYearQuarterInFlowOrder(){
		loggerLocal.info("HeadqInventoryFlowJSONAction - getYearQuarterInFlowOrder");
    	
		Response response = new Response();
		try{
		    response = headqInventoryService.getYearQuarterInOrder(formBean.getFlowOrder(), null,formBean.getJsons());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setQuickValue(Response.FAIL, e.getMessage());
		}

		jsonMap.put("response", response);

		try{
			   jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
			
				loggerLocal.error(e);
			}
		
		return SUCCESS;
	}
	
	
	/**
	 * 根据库存统计树，所选中的叶子,进行库存删除
	 * @return
	 */
	public String deleteInventory(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
    	loggerLocal.info("HeadqInventoryFlowJSONAction - deleteInventory");
		Response response = new Response();
		try{
		    response = headqInventoryService.deleteInventory(loginUserInfor,formBean.getStoreId(),formBean.getBelongTo(), formBean.getYearId(), formBean.getQuarterId(), formBean.getBrandId(), formBean.getCategoryId(), formBean.getCategoryLevelId(),formBean.getPbId());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setQuickValue(Response.FAIL, e.getMessage());
		}

		try{
			   jsonObject = JSONObject.fromObject(response);
			} catch (Exception e){
				loggerLocal.error(e);
			}
		
		return SUCCESS;		
	}


	
	

	

	
	/**
	 * 总部调货删除单据
	 * 
	 * @return
	 */
	public String deleteOrder() {
		UserInfor loginUser = (UserInfor) ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUser.getUser_name() + "headqInventoryFlowJSONAction." + "deleteOrder ");
		Response response = new Response();
		try {
			response = flowService.deleteOrderById(formBean.getFlowOrder().getId(), loginUser);

		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}finally {
			jsonObject = JSONObject.fromObject(response);
		}

		return SUCCESS;
	}
	
	
	/**
	 * 红冲单据
	 * 
	 * @return
	 */
	public String cancelOrder() {
		loggerLocal.info("HeadqInventoryFlowJSONAction - cancelOrder");
		UserInfor loginUserInfor = (UserInfor) ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);

		Response response = new Response();
		try {
			 response = flowService.cancelOrder(formBean.getFlowOrder().getId(), loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}finally {
			jsonObject = JSONObject.fromObject(response);
		}
		return SUCCESS;
	}
	
	/**
	 * 查询总部货品跟踪记录
	 * @return
	 */
	public String selectInventoryTracking() {
		loggerLocal.info("HeadqInventoryFlowJSONAction - selectInventoryTracking");
		Response response = new Response();
    			
		response = headqInventoryService.getInventoryHistory(formBean.getStartDate(),formBean.getEndDate(),formBean.getStoreId(), formBean.getBarcode());
		
		if (response.isSuccess()){
			jsonMap = (Map<String, Object>)response.getReturnValue();
		}
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes( new String[]{"productSet","productList","myChainStore","employeeUnder_Set","chainUserFunctions","toChainStore","userFunction_Set"} );
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JSONUtilDateConverter());  
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JSONSQLDateConverter());  
		try{
			   jsonObject = JSONObject.fromObject(jsonMap,jsonConfig);
  
			} catch (Exception e){
				loggerLocal.error(e);
			}
			
		return SUCCESS;	
	}
	
	
	/**
	 * 查询报溢/报损单据信息
	 * @return
	 */
	public String selectOrderProducts() {
		loggerLocal.info("HeadqInventoryFlowJSONAction - selectOrderProducts");
		
		Response response = new Response();
		try {
		    response = flowService.selectOrderProducts(formBean.getFlowOrder());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		if (response.getReturnCode() == Response.SUCCESS){
			
			jsonMap = (Map)response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
			
		return SUCCESS;	
	}
	
	/**
	 * 查询报溢/报损单据信息
	 * @return
	 */
	public String selectOrderProductDetails() {
		loggerLocal.info("HeadqInventoryFlowJSONAction - selectOrderProductDetails");
		
		Response response = new Response();
		try {
		    response = flowService.selectOrderProductDetails(formBean.getFlowOrder());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		if (response.getReturnCode() == Response.SUCCESS){
			
			jsonMap = (Map)response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
			
		return SUCCESS;	
	}
	
	

}

